package mblog.third.qiniu;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
 
/**
 * 七牛云存储图片
 * @author wuyb
 *
 */
public class QnUpLoad {
	
	public static String url="https://www.zy139.cn/";
	
	/**
	 * 上传图片
	 * @param path 上传文件路径
	 * imageName  图片名称  aaa.png
	 * @return
	 */
	public static String uploadImage(String path,long userId,String image){
		String imageName = dealImageName(userId,image);
		Configuration cfg = new Configuration(Zone.zone0());                //zone0() 代表华东地区
        UploadManager uploadManager = new UploadManager(cfg);
 
        String accessKey = "E4kwMobbsrOlZ3hNEENWPX037CUXix48NYinhLoF";      //AccessKey的值
        String secretKey = "75JqvyMfXYgVAd43z9Rfl7U_Y-NrVLM2BuKodoPc";      //SecretKey的值
        String bucket = "zy139";                                          //存储空间名
 
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);
        try {
            Response response = uploadManager.put(path, imageName, upToken);
            //解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            System.out.println(putRet.key);
            System.out.println(putRet.hash);
            System.out.println(putRet);
        } catch (QiniuException ex) {
            Response r = ex.response;
            System.err.println(r.toString());
            try {
                System.err.println(r.bodyString());
            } catch (QiniuException ex2) {
                //ignore
            }
        }
        return url+imageName;
	}
	
	private static String dealImageName(long userId,String imageName){
		imageName=imageName.replaceAll(" ","");
//		imageName=imageName.replaceAll("]","");
//		imageName=imageName.replaceAll("[","");  //!@#
		imageName=imageName.replaceAll("!","");
		imageName=imageName.replaceAll("@","");
		imageName=imageName.replaceAll("#","");
		imageName=imageName.replaceAll("$","");
		imageName=imageName.replaceAll("&","");
		return userId+"="+System.currentTimeMillis()+"="+imageName;
	}
 
    public static void main(String[] args) {
        Configuration cfg = new Configuration(Zone.zone0());                //zone0() 代表华东地区
        UploadManager uploadManager = new UploadManager(cfg);
 
        String accessKey = "E4kwMobbsrOlZ3hNEENWPX037CUXix48NYinhLoF";      //AccessKey的值
        String secretKey = "75JqvyMfXYgVAd43z9Rfl7U_Y-NrVLM2BuKodoPc";      //SecretKey的值
        String bucket = "zy139";                                          //存储空间名
        String localFilePath = "G:\\1111\\java125221228.png";     //上传图片路径
 
        String key = "456.png";                                               //在七牛云中图片的命名
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);
        try {
            Response response = uploadManager.put(localFilePath, key, upToken);
            //解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            System.out.println(putRet.key);
            System.out.println(putRet.hash);
            System.out.println(putRet);
        } catch (QiniuException ex) {
            Response r = ex.response;
            System.err.println(r.toString());
            try {
                System.err.println(r.bodyString());
            } catch (QiniuException ex2) {
                //ignore
            }
        }
}
    }
